<?php
namespace Wap\Controller;

use Think\Controller;
use Think\Image;
use Think\Model;
use User\Model\CardModel;
use User\Model\CardRegisterModel;
use User\Model\FactoryModel;
use Wap\Service\WeixinService;

//header('Access-Control-Allow-Origin: *');

class CardController extends WapController
{

    public $_card;//活动信息
    public $wxuser;//活动绑定的微信号
    public $user;//微信用户
    public $is_mobile;

    public function __construct()
    {
        parent::__construct();

        if (!headers_sent() && // 如果页面头部信息还没有输出--
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展--
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP--
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }

        $this->handleCriteriaMember();
        $this->handleCriteriaWxuser();
        $this->is_mobile = $this->isMobile();
        if ($this->is_mobile) {
            $wxJSApi = WeixinService::buildWXJSApi($this->wxuser);
            /*if(empty($wxJSApi)){
                header("Content-type: text/html; charset=utf-8");
                die('服务器繁忙请稍后！');
            }*/
            $this->assign('nonceStr', $wxJSApi['noncestr']);
            $this->assign('wxTicket', $wxJSApi['ticket']);
            $this->assign('timestamp', $wxJSApi['timestamp']);
            $this->assign('wxSha1', $wxJSApi['sha1']);

            $this->assign("wxuser", $this->wxuser);
        }
        $this->assign("card", $this->_card);
    }

    /**
     *函数名: index
     *函数描述: 获取微信网页静默授权CODE、加载所有作品主页模板
     * @param string $_GET ['code'];
     * @return object 模板
     */
    public function index()
    {
        return $this->display("index_new");
    }

    public function register()
    {
        $params = I("post.", "", "");
        if (empty($params['id'])) {
            return $this->ajaxReturn(array('status' => -1, "result" => "无效的卡片项目"));
        }
        if (empty($params['pic'])) {
            return $this->ajaxReturn(array('status' => -2, "result" => "请上传图片"));
        }
        $data['card_id'] = $params['id'];
        $data['token'] = $this->wxuser['token'];
        $data['name'] = $params['name'] ? $params['name'] : '';
        $img_upload = FactoryModel::ajaxImgUpload($params['pic'], 'card');
        if ($img_upload['code'] != 1) {
            return $this->ajaxReturn(array('status' => -3, "result" => $img_upload['msg']));
        }
        $root = substr($img_upload['msg'], 1);
        $image = new Image();
        $image->open($root);
        $width = $image->width() / 8;
        $height = $image->height() / 8;

        $explode = explode(".", $root);
        $rename = $explode[0] . 'thumb.' . $explode[1];
        $image->thumb($width, $height)->save($rename);
        $data['picurl'] = $img_upload['msg'];
        $data['mobile'] = $params['mobile'] ? $params['mobile'] : '';
        $data['status'] = CardRegisterModel::STATUS_ACTIVE;
        $data['content'] = $params['content'];
        $reg_id = M("card_register")->add($data);
        return $this->ajaxReturn(array('status' => 1, "result" => $reg_id));
    }

    public function share()
    {
        return $this->display();
    }

    public function intro()
    {
        return $this->display();
    }

    public function mobile()
    {
        $reg_id = I("get.reg_id", "", "");
        $reg = "";
        if (!empty($reg_id)) {
            $reg = M("card_register")->where(['reg_id' => $reg_id])->find();
        }
        $this->assign("reg", $reg);
        $self = I("get.self", "", "");
        $this->assign("self", $self);
        return $this->display();
    }

    /*
    *是否合法活动
    */
    private function handleCriteriaMember()
    {
        $cond['card_id'] = IS_GET ? I("get.id", -1, "") : I("post.id", -1, "");
        $cond['status'] = CardModel::STATUS_ACTIVE;
        $this->_card = M("card")->where($cond)->find();
        if (empty($this->_card)) {
            header("Content-type: text/html; charset=utf-8");
            die('无效的卡片！');
        }
    }

    /*
    *微信公众号信息
    */
    private function handleCriteriaWxuser()
    {
        $activity = $this->_card;
        if (isset($activity['token'])) {
            $this->wxuser = WeixinService::getWxuser($this->_card['token']);
        }
        if (empty($this->wxuser)) {
            header("Content-type: text/html; charset=utf-8");
            die('无效的卡片！!');
        }
    }
}
